﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLExcel
{
    public partial class MainPage : UserControl
    {

        public List<TextBox> TextBoxs = new List<TextBox>();
        public List<TextBlock> TextBlocks = new List<TextBlock>();
        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            TextBoxs.Clear();
            CreateCell(sheet1_grid);
        } 
         
        void  textBox_LostFocus(object sender, RoutedEventArgs e)
        { 
        }

        void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = (e.OriginalSource as TextBox);
            foreach (var tb in TextBoxs)
            {
                var border = tb.Parent as Border;
                if (tb.Name != textBox.Name)
                {
                    border.SetValue(Border.BorderBrushProperty, new SolidColorBrush(Colors.LightGray));
                    border.SetValue(Border.BorderThicknessProperty, new Thickness(0, 0, 1, 1));  
                }
                else
                {
                    border.SetValue(Border.BorderBrushProperty, new SolidColorBrush(Colors.Black));
                    border.SetValue(Border.BorderThicknessProperty, new Thickness(2,2,2,2)); 
                }
            } 
        }
         
        void TextBlock_KeyUp(object sender, KeyEventArgs e)
        {
            char dd = Convert.ToChar(e.Key);
            
            var textBlock = e.OriginalSource as TextBlock;
            textBlock.Text += dd.ToString();
        }

        private void TextBlock_LostFocus(object sender, RoutedEventArgs e)
        { 
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {

        }

        private void sheet1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && (e.AddedItems[0] as TabItem) != null)
            {
                var selectedItem = (e.AddedItems[0] as TabItem);
                if (selectedItem.Name == "newSheetTabItem")
                {
                    TabItem newTabItem = new TabItem();
                    newTabItem.Header = string.Format("sheet{0}", sheetTabControl.Items.Count);
                    sheetTabControl.Items.Insert(sheetTabControl.Items.Count - 1, newTabItem); ;
                    CreateTabItemContent(newTabItem);
                    sheetTabControl.SelectedItem = newTabItem;
                }
            }
        }

        void CreateTabItemContent(TabItem tabItem)
        {
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.LightGray);
            border.BorderThickness = new Thickness(1, 1, 1, 1);
            Grid grid = new Grid();
            CreateCell(grid);
            border.Child = grid;
            tabItem.Content = border;
        }

        void CreateCell(Grid grid)
        {
            grid.RowDefinitions.Clear();
            grid.ColumnDefinitions.Clear();
            var width = this.sheetTabControl.Width;
            var height = this.sheetTabControl.Height;
            var textBoxWidth = 100;
            var textBoxHeight = 20;
            var columnCount = Math.Ceiling(width / textBoxWidth);
            var rowCount = Math.Ceiling(height / textBoxHeight);
            for (int i = 0; i < rowCount; i++)
            {
                grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
            }
            for (int j = 0; j < columnCount; j++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
            } 
            grid.Children.Clear();
            for (int i = 0; i < columnCount; i++)
            {
                for (int j = 0; j < rowCount; j++)
                {
                    Border border = new Border();
                    border.BorderBrush = new SolidColorBrush(Colors.LightGray);
                    border.BorderThickness = new Thickness(.5);
                    border.SetValue(Grid.ColumnProperty, i);
                    border.SetValue(Grid.RowProperty, j);
                    TextBoxNoFocus textBox = new TextBoxNoFocus();
                    textBox.BorderThickness = new Thickness(0);
                    textBox.Width = 100;
                    textBox.Height = 20;
                    textBox.Margin = new Thickness(0);
                    textBox.Padding = new Thickness(0);
                    textBox.Name = string.Format("TextBox{0}{1}", i, j);
                    border.Child = textBox;
                    textBox.GotFocus += new RoutedEventHandler(textBox_GotFocus);
                    textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus);
                    TextBoxs.Add(textBox);
                    grid.Children.Add(border);
                }
            } 
        }
    }
}
